<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!--Hier fuegt man seinen Arbeitsgeber  oder Firmennamen ein und beschreibt von wann bis wann man in der Firma gearbeitet hat... -->
<%String file="./index.jsp?"+Constant.CONTENT+"="+Constant.school, method="get"; %>
<%@page import="db.DBMain"%>
<%RequestParser.send_action_file=file;
RequestParser.send_method=method;%>
<%@include file="town.jsp" %>
<%@page import="html.RequestParser"%>
<%@page import="db.tables.DBTypeTable"%>
<%@page import="java.sql.Date"%>
<%@page import="db.tables.DBSchoolTable"%>
<%@page import="db.tables.reference.DBScholarAtTable"%>
<script type="text/javascript"><!--

<%
Long type_id_temp=DBMain.getTypeTable().selectLong(DBTypeTable.TYPE_ID_COL,DBTypeTable.TYPE_NAME_COL,Constant.Type.SCHOOL),
	user_id=0L, cat_id=RequestParser.getLongFromParameter(request,Constant.Content.CATEGORY);//session.getAttribute(Constant.USERID);
session.setAttribute(Constant.Content.TYPE,type_id_temp);

RequestParser schoolParser=new RequestParser(request,file,method);
schoolParser.setAdd(Constant.Content.ADD_SCHOOL);
schoolParser.setContent(Constant.Content.SCHOOL);
schoolParser.setSubmit(Constant.Content.SUBMIT_SCHOOL);
schoolParser.setDelete(Constant.Content.DELETE_SCHOOL);
schoolParser.setInput(new String[]{Constant.Content.INPUT_SCHOOL,Constant.Content.TOWN,Constant.Content.CATEGORY});
schoolParser.setInsert_columns(new String[]{DBSchoolTable.SCHOOL_NAME_COL,DBSchoolTable.TOWN_ID_COL,DBSchoolTable.CATEGORY_ID_COL});
schoolParser.setDisplay_columns(new String[]{DBSchoolTable.SCHOOL_ID_COL,DBSchoolTable.SCHOOL_NAME_COL,DBSchoolTable.CATEGORY_ID_COL});
schoolParser.setPk_column(DBSchoolTable.SCHOOL_ID_COL);
schoolParser.setTable(DBMain.getSchoolTable());
schoolParser.setFk_columns(new String[]{DBSchoolTable.TOWN_ID_COL,DBSchoolTable.CATEGORY_ID_COL});
schoolParser.setFk_ids(new Long[]{town_id,cat_id});
schoolParser.process();

RequestParser scholarAtParser=new RequestParser(request,file,method);
scholarAtParser.setAdd(Constant.Content.ADD_SCHOLAR_AT);
scholarAtParser.setContent(Constant.Content.SCHOLAR_AT);
scholarAtParser.setSubmit(Constant.Content.SUBMIT_SCHOLAR_AT);
scholarAtParser.setDelete(Constant.Content.DELETE_SCHOLAR_AT);
scholarAtParser.setInput(new String[]{Constant.Content.SCHOOL,Constant.Content.INPUT_START,Constant.Content.INPUT_END});
scholarAtParser.setInsert_columns(new String[]{DBScholarAtTable.USER_ID_COL,DBScholarAtTable.SCHOOL_ID_COL,DBScholarAtTable.START_DATE_COL,DBScholarAtTable.END_DATE_COL});
scholarAtParser.setDisplay_columns(new String[]{DBScholarAtTable.SCHOLAR_AT_ID_COL,DBScholarAtTable.START_DATE_COL,DBScholarAtTable.END_DATE_COL});
scholarAtParser.setPk_column(DBScholarAtTable.SCHOLAR_AT_ID_COL);
scholarAtParser.setTable(DBMain.getScholarAtTable());
scholarAtParser.setFk_column(DBScholarAtTable.USER_ID_COL);
scholarAtParser.setFk_id(user_id);
scholarAtParser.setPk_id(RequestParser.getLongFromParameter(request, scholarAtParser.getContent()));

	// #########################################################
	// FIXME edit so it is only available to admins!
	// #########################################################
	if (request.getParameter(scholarAtParser.getSubmit()) != null) {
		if (RequestParser.areAllParametersNotNull(request, scholarAtParser.getInput())) {
			int i=0;
			String[] req=scholarAtParser.getInput();
			Object[] result = new Object[req.length+1];
			//DBScholarAtTable.USER_ID_COL 
			result[i]=user_id;
			//DBScholarAtTable.SCHOOL_ID_COL
			i++;
			result[i]=Long.parseLong(request.getParameter(Constant.Content.SCHOOL));
			//DBScholarAtTable.START_DATE_COL,
			i++;
			result[i]=Date.valueOf(request.getParameter(Constant.Content.INPUT_START));
			//DBScholarAtTable.END_DATE_COL 
			i++;
			String t=request.getParameter(Constant.Content.INPUT_END);
			if(t!=null&&!t.equals("")&&!t.equalsIgnoreCase("null"))
				result[i]=Date.valueOf(t);
			else result[i]=null;
			scholarAtParser.setPk_id(scholarAtParser.getTable().insertGetPK(scholarAtParser.getInsert_columns(), result));
		} else if (RequestParser.areSomeParametersNotNull(request, scholarAtParser.getInput())) {
			// TODO Error output because only one parameter is set -
			// needed are both!
		}
	}

	// #########################################################
	// FIXME add additional functions so it is i.e. possible to delete
	// only all related towns or vice versa
	// and same as above: only for Admins!!
	// #########################################################
	if (request.getParameter(scholarAtParser.getDelete()) != null && scholarAtParser.getPk_id() != null) {
		scholarAtParser.getTable().delete(scholarAtParser.getPk_column(), scholarAtParser.getPk_id());
	}

	scholarAtParser.setArray(scholarAtParser.getTable().getSelect(scholarAtParser.getDisplay_columns(), new String[]{scholarAtParser.getFk_column()}, new Object[]{scholarAtParser.getFk_id()}));
%>
//
--></script>
<form name="<%=schoolParser.getContent() %>" action="<%=schoolParser.getSend_action_file() %>" method="<%=schoolParser.getSend_method()%>">
	<%@include file="category_only.jsp" %>
	<select name="<%=Constant.Content.SCHOOL %>" onchange="submit()">
		<%
			schoolParser.printOptionList(out);
		%>
	</select>
          <table width="38%" border="0">
       <tr> 
         <td width="17%">Beginn:</td>
         <td width="83%">
           <input type="text" name="<%=Constant.Content.INPUT_START%>" size="11" value="">
		<a href="javascript: void(0);" onmouseover="if (timeoutId) clearTimeout(timeoutId);window.status='Show Calendar';return true;" 
		onmouseout="if (timeoutDelay) calendarTimeout();window.status='';" onclick="g_Calendar.show(event,'<%=schoolParser.getContent() %>.<%=Constant.Content.INPUT_START%>',true,'yyyy-mm-dd'); return false;">
		<img src="../images/calendar.gif" name="imgCalendar" width="34" height="21" border="0" alt=""></a>
         </td>
         </tr>
             <tr> 
         <td width="17%">Ende: </td>
         <td width="83%">
           <input type="text" name="<%=Constant.Content.INPUT_END%>" size="11" value="">
		<a href="javascript: void(0);" onmouseover="if (timeoutId) clearTimeout(timeoutId);window.status='Show Calendar';return true;" 
		onmouseout="if (timeoutDelay) calendarTimeout();window.status='';" onclick="g_Calendar.show(event,'<%=schoolParser.getContent() %>.<%=Constant.Content.INPUT_END%>',true,'yyyy-mm-dd'); return false;">
		<img src="../images/calendar.gif" name="imgCalendar" width="34" height="21" border="0" alt=""></a>
         </td>
         </tr>
             <tr>
         <td width="17%">
         	<input type="hidden" name="<%=Constant.Content.COUNTRY %>" value="<%=country_id %>"/>
			<input type="hidden" name="<%=Constant.Content.TOWN %>" value="<%=town_id %>"/> 
           <input type="submit" name="<%=Constant.Content.SUBMIT_SCHOLAR_AT %>" value="Speichern">
         </td>
         <td width="83%">
           <input type="reset" name="zuruecksetzen" value="Zur&uuml;cksetzen">
         </td>
     </table>
</form>